package com.example.learn_spring_boot3.controller;

import com.example.learn_spring_boot3.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Api(tags = "测试接口", description = "测试使用的接口")
@RestController
@RequestMapping("/test")
public class TestController {
    @ApiOperation(value = "测试接口", notes = "测试接口信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
    @GetMapping("/hello")
    public ResponseEntity<String> test(@RequestParam(required = false) String name) {
        System.out.println("name: " + name);
        return ResponseEntity.ok("ok");
    }

    // 1. 使用@RequestParam获取请求参数
    @GetMapping("/getUserWithRequestParam")
    public ResponseEntity<HashMap<String, Object>> getUserWithRequestParam(@RequestParam String name, @RequestParam int age) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("age", age);
        return ResponseEntity.ok(map);
    }

    // 2. 使用@PathVariable获取路径中的参数
    @GetMapping("/getUserWithPathVariable/{id}")
    public ResponseEntity<HashMap<String, Object>> getUserWithPathVariable(@PathVariable String id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", id);
        return ResponseEntity.ok(map);
    }

    // 3. 使用@RequestBody获取请求体中的数据
    @PostMapping("/createUserWithRequestBody")
    public ResponseEntity<User> createUserWithRequestBody(@RequestBody User user) {
        return ResponseEntity.ok(user);
    }


    // 4. 使用@ModelAttribute获取表单提交的数据
    @PostMapping("/createUserWithModelAttribute")
    public ResponseEntity<User> createUserWithModelAttribute(@ModelAttribute User user) {
        return ResponseEntity.ok(user);
    }

    // 5. 使用@RequestHeader获取请求头数据
    @PostMapping("/getUserWithRequestHeader")
    public ResponseEntity<String> getUserWithRequestHeader(@RequestHeader("User-Agent") String userAgent) {
        return ResponseEntity.ok(userAgent);
    }

    // 6. 使用POJO类对象传参
    @PostMapping("/createUser")
    public ResponseEntity<User> createUser(User user) {
        return ResponseEntity.ok(user);
    }
}
